package edu.uic.ids517.servlet;
import java.security.*;
import java.io.IOException;
import java.math.BigInteger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.uic.ids517.model.DatabaseAccessBean;
import edu.uic.ids517.model.DbLoginBean;
import edu.uic.ids517.model.SignUpBean;


public class SignUpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("SignUpServlet");
		String successUrl = "/SignUpSuccess.jsp";
		String failureUrl = "/Error.jsp";
		HttpSession session = request.getSession();
		
		String uin = request.getParameter("uin");
		String userName = request.getParameter("uname");
		String plainTextPassword = request.getParameter("pass");
		String firstName = request.getParameter("fname");
		String lastName = request.getParameter("lname");
		
		if(uin.length() == 0 || userName.length() == 0 || plainTextPassword.length() == 0 || firstName.length() == 0 || lastName.length() == 0){
			RequestDispatcher dispatcher = request.getRequestDispatcher(failureUrl);
			request.setAttribute("errorMessage", "Signup Failure");
			dispatcher.forward(request, response); 
		}		

		/***   DbLoginBean  ****/
		DbLoginBean dbInfo = (DbLoginBean) session.getAttribute("dbmsUserBean");
		dbInfo.setSessionId(session.getId());
		
		/***   SignUp Bean  ****/
		SignUpBean signupInfo = (SignUpBean) session.getAttribute("signupBean");
		signupInfo.setUsername(userName);

		
		//password encryption
		MessageDigest m;
		String hashtext = "password";
		try {
			m = MessageDigest.getInstance("MD5");
			m.reset();
			m.update(plainTextPassword.getBytes());
			byte[] digest = m.digest();
			BigInteger bigInt = new BigInteger(1,digest);
			hashtext = bigInt.toString(16);
		} catch (NoSuchAlgorithmException e) {
			System.out.println("Encryption Failed");
			e.printStackTrace();
		}

		signupInfo.setPassword(hashtext);
		//signupInfo.setPassword(request.getParameter("pass"));
		signupInfo.setUin(request.getParameter("uin"));
		signupInfo.setFirstName(request.getParameter("fname"));
		signupInfo.setLastName(request.getParameter("lname"));

		DatabaseAccessBean dbase = (DatabaseAccessBean) session.getAttribute("dbaseBean"); 
		dbase.setSignupBean(signupInfo); 
		dbase.connect();		
		
		int result = dbase.signUpCreation();
		
		
		
		if(result == 0){
			System.out.println("SUCCESS: USER CRETATED");

			RequestDispatcher dispatcher = request.getRequestDispatcher(successUrl); 
			dispatcher.forward(request, response); 
		}
		else{
		RequestDispatcher dispatcher = request.getRequestDispatcher(failureUrl);
		request.setAttribute("errorMessage", "Signup Failure");
		dispatcher.forward(request, response); 
		}

	}

}
